$(function(){
    //未登录跳入登录页面
    var hasLogin=Utils.getCookie("hasLogin");
    if(!hasLogin){
        location.href="./login.html";
     }
     //设置当前城市
    var currentCity;
    function setAddress(){
        var addr=Utils.getCookie("currentCity");
        if(addr){
            currentCity=addr;
            $("#currentCity").html(addr);
             $(".changecity span").html(addr);
        }else{
            currentCity=Utils.getCurrentCity("currentCity");
            $("#currentCity").html(currentCity); 
            $(".changecity span").html(currentCity);
        }
    }
    setAddress();
    //获取userId
    var userId;
    function setUserid(){
        var userid=Utils.getCookie("userId");
        if(userid){
            userId=userid;
        }else{
            userId=Utils.getQueryString("userId");
        }
    }
    setUserid();
    
    console.log(userId+"userId");
    //跳转进入选择地址
    $(".changecity a").on("click",function(){
        location.href="./address.html?currentCity="+currentCity;
    });
    
    function jump_page(ele){
        var ele=ele;
        $(ele).on("click",function(){
            var links=$(this).attr("data-links");
            location.href="./"+links+".html?currentCity="+currentCity;
        });   
    }
    jump_page(".jump_links");
    
    $(".nav_down").on("click",function(){
        if($(".choose-bar").css("display")=="none"){
            $(".choose-bar").css("display","block");
            $(".classifynav .nav_down i").addClass("bgup").removeClass("bgdown");
        }else{
            $(".choose-bar").css("display","none");
            $(".classifynav .nav_down i").addClass("bgdown").removeClass("bgup");
        }
    });
    $(".zwl-first").on("click",function(){
        location.href="./myService.html?currentCity="+currentCity;
    });
    $(".zwl-second").on("click",function(){
        location.href="./myShopping.html?currentCity="+currentCity;
    });
    
    
    //数据展示
    var currentPage=1;//默认设置当前页为第一页
        var totalPage=1;//设置默认总页数
        var show_count=15;//设置默认每次加载的条数
        var resType=1;//默认类型
        var url=Utils.reqUrl,//请求url
        async=true,
        type="post";
    var searchName="";
    //获取我的服务数据的接口
        var getMyServiceList=JSON.stringify({
            "code":"getLocalService",
            "version":"1.0",
            "data":{
                "address":currentCity,
                "productType":"SERVICE",
                "serviceId":"",
                "userId":userId,
                "productName":"",
                "resType":resType,
                "currentPage":currentPage,
                "showCount":show_count,
                "productName":searchName
            }
        });
        
        getData(url,getMyServiceList,call_func_servicetotalpage,async,type);
    //ajax获取数据
        function getData(url,datas,call_func,async,type){
            console.log("请求参数:"+datas);
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                async:async,
                contentType:"application/json",
                data:datas,
                success:function(data){
                    call_func(data);
                },
                error:function(msg){
                    console.log(msg);
                }         
            });        
        }
    //获取服务总页数
        function call_func_servicetotalpage(data){
            console.log(data);
            totalPage=data.data.totalPage;
             page();
        }
     //获取服务数据的回调函数
        function call_func_service(data){
            console.log(data);
            var serv_datas=data.data.datas;//数据列
            var serv_len=serv_datas.length;//数据列个数
            var servStrs='';
            for(var i=0;i<serv_len;i++){
                servStrs+='<li data-id="'+serv_datas[i].productId+'" class="marg_l10 ser_li">'
                                +'<div class="data_lable_div"></div>'
                                +'<div class="shadow_bg"></div>'
                                +'<div class="shadow_del"><span class="sha_type">'+serv_datas[i].serviceName+'</span><img class="sha_img" src="./images/collect.png" ><span class="sha_cout">'+serv_datas[i].serviceCollectCount+'</span><img class="sha_img" src="./images/view.png" ><span class="sha_cout">'+serv_datas[i].viewCount+'</span></div>'
                                +'<img class="data_img lazy" title="'+serv_datas[i].productName+'"  data-original="'+serv_datas[i].userImgPath+'" src="./images/loading.gif">'
                                +'<div class="pro_mark">'+serv_datas[i].productName+'</div>'
                                +'<div class="pro_detail">'
                                    +'<p>'+serv_datas[i].remark+'</p>'
                                +'</div>'

                                +'<div class="pro_lable">'
                                   +'<img class="pro_rz_img" src="./images/renzheng.png" ><i class="pro_detail_name">'+serv_datas[i].serviceName+'</i>'
                                    +'<lable>'
                                        +'<span class="lab_price">￥'+serv_datas[i].saleprice+'</span>'
                                        +'/<span class="lab_unit">'+serv_datas[i].unit+'</span>'
                                    +'</lable>'
                                +'</div>'
                            +'</li>';
            }
            $(".serviceList").html(servStrs);
                $("img.lazy").lazyload({
                threshold : 200
            });
           
            //点击当前服务，跳转进入商家详情页
            $(".serviceList li").on("click",function(){
                var serviceId=$(this).attr("data-id");
                location.href="./serviceDetail.html?productId="+serviceId;
            });
        }
    
    //按照服务名搜索服务
    $(".button").on("click",function(){
        searchName=$(".search-bar input").val();
        page();
    });
    
    //分页操作
    function page(){
        layui.use(['laypage', 'layer'], function(){
              var laypage = layui.laypage,
                  layer = layui.layer;
                  laypage({
                    cont: 'page',
                      pages: totalPage,
                      skip: true,
                      first:false,
                      lase:false,
                      jump: function(obj, first){
                         currentPage = obj.curr;
                         var getpageServiceList=JSON.stringify({
                            "code":"getLocalService",
                            "version":"1.0",
                            "data":{
                                "address":currentCity,
                                "productType":"SERVICE",
                                "serviceId":"",
                                "userId":userId,
                                "productName":"",
                                "resType":resType,
                                "currentPage":currentPage,
                                "showCount":show_count,
                                "productName":searchName
                            }
                        });
                        getData(url,getpageServiceList,call_func_service,async,type);
                      }
                  });
            });
    }
});